﻿$(document).ready(function () {
    setTimeout(checkUpdate, 10);
    // WatermarkedInput
    $(".watermarkedInput input:text,.watermarkedInput input:password,.watermarkedInput textarea").each(function () {
        $(this).focus(function () {
            if (this.value == "") { $(this).next("label").hide(); }
        });

        $(this).blur(function () {
            if (this.value == "") { $(this).next("label").show(); }
        });

        if (!$(this).val() == "") { $(this).next("label").hide(); }
    });
    //CustomCheckBox
    $(".customCheckbox").each(function () {
        $(this).click(function () {
            if ($(this).next("input:hidden").val() == "false") {
                $(this).next("input:hidden").val("true");
                $(this).css('backgroundPosition', '0 -20px');
            }
            else {
                $(this).next("input:hidden").val("false");
                $(this).css('backgroundPosition', '0 0px');
            }
        });
        if ($(this).next("input:hidden").val() == "false") {
            $(this).css('backgroundPosition', '0 0px');
        }
        else {
            $(this).css('backgroundPosition', '0 -20px');
        }
    });
    $(".customfileinput").each(function () {
        $(this).mouseover(function () { $(this).parent().addClass('customfilehover'); })
               .mouseout(function () { $(this).parent().removeClass('customfilehover'); })
               .focus(function () {
                   $(this).parent().addClass('customfilefocus');
                   $(this).data('val', $(this).val());
               })
                                 .blur(function () {
                                     $(this).parent().removeClass('customfilefocus');
                                     $(this).trigger('checkChange');
                                 })
                                  .bind('disable', function () {
                                      $(this).attr('disabled', true);
                                      $(this).parent().addClass('customfiledisabled');
                                  })
                                .bind('enable', function () {
                                    $(this).removeAttr('disabled');
                                    $(this).parent().removeClass('customfiledisabled');
                                })
                                .bind('checkChange', function () {
                                    if ($(this).val() && $(this).val() != $(this).data('val')) {
                                        $(this).trigger('change');
                                    }
                                })
                                .bind('change', function () {
                                    //get file name
                                    var fileName = $(this).val().split(/\\/).pop();
                                    //get file extension
                                    var fileExt = 'customfileext-' + fileName.split('.').pop().toLowerCase();
                                    //update the feedback
                                    $(this).prev(".customfilefeedback").text(fileName)
                                                  .removeClass($(this).prev(".customfilefeedback").data('fileExt') || '')
                                                  .addClass(fileExt)
                                                  .data('fileExt', fileExt)
                                                  .addClass('customfilefeedbackpopulated');
                                    //change text of button
                                })
                                .click(function () { //for IE and Opera, make sure change fires after choosing a file, using an async callback
                                    $(this).data('val', $(this).val());
                                    setTimeout(function () {
                                        $(this).trigger('checkChange');
                                    }, 100);
                                });
    });
    $(".customFileUploadDiv").each(function () {
        $(this).mousemove(function (e) {
            $(this).find(".customfileinput")
                        .css({
                            'left': e.pageX - $(this).offset().left - $(this).find(".customfileinput").outerWidth() + 20, //position right side 20px right of cursor X)
                            'top': e.pageY - $(this).offset().top - $(window).scrollTop() - 3
                        });
        });
    });
    if ($("#dialog").html() != null) {
        $("#dialog").dialog({
            autoOpen: false,
            modal: true
        });
    }
    $(".delete").each(function () {
        $(".delete").click(function (e) {
            e.preventDefault();
            var theHREF = $(this).attr("href");

            $("#dialog").dialog('option', 'buttons', {
                "Confirm": function () {
                    window.location.href = theHREF;
                },
                "Cancel": function () {
                    $(this).dialog("close");
                }
            });

            $("#dialog").dialog("open");

        });
    });
    function checkUpdate() {
        setTimeout(checkUpdate, 10);
        $(".watermarkedInput input:text,.watermarkedInput input:password").each(function () {
            if (!$(this).val() == "") { $(this).next("label").hide(); }
        });
    }
});
///**
//* --------------------------------------------------------------------
//* customfileinput
//* --------------------------------------------------------------------
//*/
//$.fn.customFileInput = function () {
//    return $(this).each(function () {
//        //apply events and styles for file input element
//        var fileInput = $(this)
//.addClass('customfileinput') //add class for CSS
//.mouseover(function () { upload.addClass('customfilehover'); })
//.mouseout(function () { upload.removeClass('customfilehover'); })
//.focus(function () {
//    upload.addClass('customfilefocus');
//    fileInput.data('val', fileInput.val());
//})
//.blur(function () {
//    upload.removeClass('customfilefocus');
//    $(this).trigger('checkChange');
//})
//.bind('disable', function () {
//    fileInput.attr('disabled', true);
//    upload.addClass('customfiledisabled');
//})
//.bind('enable', function () {
//    fileInput.removeAttr('disabled');
//    upload.removeClass('customfiledisabled');
//})
//.bind('checkChange', function () {
//    if (fileInput.val() && fileInput.val() != fileInput.data('val')) {
//        fileInput.trigger('change');
//    }
//})
//.bind('change', function () {
//    //get file name
//    var fileName = $(this).val().split(/\\/).pop();
//    //get file extension
//    var fileExt = 'customfile-ext-' + fileName.split('.').pop().toLowerCase();
//    //update the feedback
//    uploadFeedback
//.text(fileName) 
//.removeClass(uploadFeedback.data('fileExt') || '')
//.addClass(fileExt) 
//.data('fileExt', fileExt) 
//.addClass('customfile-feedback-populated'); 
//    //change text of button
//})
//.click(function () { //for IE and Opera, make sure change fires after choosing a file, using an async callback
//    fileInput.data('val', fileInput.val());
//    setTimeout(function () {
//        fileInput.trigger('checkChange');
//    }, 100);
//});
//        //create custom control container
//        var upload = $('<div class="customfile"></div>');
//        //create custom control button
//        var uploadButton = $('<span class="customfile-button" aria-hidden="true">Browse</span>').appendTo(upload);
//        //create custom control feedback
//        var uploadFeedback = $('<span class="customfile-feedback" aria-hidden="true">No file selected...</span>').appendTo(upload);
//        //match disabled state
//        if (fileInput.is('[disabled]')) {
//            fileInput.trigger('disable');
//        }
//        //on mousemove, keep file input under the cursor to steal click
//        upload
//.mousemove(function (e) {
//    fileInput.css({
//        'left': e.pageX - upload.offset().left - fileInput.outerWidth() + 20, //position right side 20px right of cursor X)
//        'top': e.pageY - upload.offset().top - $(window).scrollTop() - 3
//    });
//})
//.insertAfter(fileInput); //insert after the input
//        fileInput.appendTo(upload);
//    });
//};